import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ProviderPage extends StatelessWidget {
  const ProviderPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var read = context.read<Model1>();
    var watch = context.watch<Model1>();
    // 定义 ValueNotifier 对象 _counter
    final ValueNotifier<int> _counter = ValueNotifier<int>(0);
    return Scaffold(
      appBar: AppBar(
        title: Text("provider_example"),
        centerTitle: true,
      ),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.only(top: 10),
        child: DefaultTextStyle(
          style: TextStyle(fontSize: 50, color: Colors.black),
          child: Column(
            children: [
              FloatingActionButton(
                child: Icon(Icons.exposure_minus_1),
                onPressed: () => read.miuns(),
              ),
              Text(
                "${watch.num}",
              ),
              FloatingActionButton(
                child: Icon(Icons.add),
                onPressed: () => read.add(),
              ),
              ValueListenableBuilder<int>(
                builder: (BuildContext context, int value, Widget? child) {
                  return Text("Value: $value");
                },
                valueListenable: _counter,
              ),
              FloatingActionButton(
                child: Icon(Icons.add_to_drive),
                onPressed: () => _counter.value++,
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => read.add(),
        child: Icon(Icons.add, size: 30),
      ),
    );
  }
}

class Model1 with ChangeNotifier {
  int num = 0;

  void add() {
    num++;
    notifyListeners();
  }

  void miuns() {
    num--;
    notifyListeners();
  }
}
